<?php
namespace app\index\controller;
//导入Controller
use think\Controller;

//导入数据库
use think\Db;
use think\Session;
session_start();
class Pay extends Allow{
	public function getindex(){
		// var_dump($_SESSION['order']);
		//品牌遍历
		if(isset($_SESSION['order'])){
      	$brand=Db::table("brand")->select();
		//地址信息
 		$address=Db::table("address")->where("user_id",Session::get('uid'))->select();
 		// var_dump($_SESSION);
 		$allprice="";
 		foreach($_SESSION['order'] as $k=>$v){
 			$allprice+=$v['num']*$v['price'];
 		}
 		//查询优惠卷
 		$coupon=Session::get('coupon');
 		$price=Db::table('coupon')->where('id',"{$coupon}")->find();

		//加载确认订单模版
		return $this->fetch("Pay/pay",['cart'=>$_SESSION['order'],'address'=>$address,'allprice'=>$allprice,'brand'=>$brand,'price'=>$price,'coupon'=>$coupon]);
	}else{
		 $this->error("未选中购物车内商品","/cart/index");
	}

}

	public function gettwo(){
		$request=request();
       //获取商品id
        $upid=$request->param('upid');
	 //基本操作
		// $sql="SELECT * FROM district WHERE upid={$_GET['upid']}";

	 //获取所有数据
	   $list=Db::table("district")->where("upid","{$upid}")->select();
		echo json_encode($list);
	}

	public function postinsertaddress(){
		$request=request();
		$data=$request->only(['name','phone','huo','adds']);
		$data['user_id']=Session::get('uid');
		//执行插入
		Db::table("address")->insert($data);
		 $this->redirect("/pay/index");
	}

	public function getaddorder(){
		$request=request();
		//地址id
		$aid=$request->param("id");
		//订单号
		$ordernum=date('Ymd',time()).mt_rand(1000,9999);
		//用户id
		$userid=Session::get('uid');
		//通过地址id查询地址信息
		$data=Db::table("address")->where("id","{$aid}")->select();
		// var_dump($data);die;
		//收件人姓名
		$oname=$data[0]['name'];
		//收件人电话
		$phone=$data[0]['phone'];
		//总价
		$allprice="";
 		foreach($_SESSION['order'] as $k=>$v){
 			$allprice+=$v['num']*$v['price'];
 		}
 		//地址
 		$address=$data[0]['huo'].$data[0]['adds'];
 		//插入order表
 		// $list=array($ordernum,$userid,$oname,$phone,$address,$allprice);
 		//  		//查询优惠卷信息
 		$coupon=Session::get('coupon');
 		if(isset($coupon)){
 			$idp=Session::get('coupon');
	 		$p=Db::table('coupon')->where('id',"{$idp}")->find();
	 		$prices=$p['price'];
 			$list['allprice']=$allprice-$prices;
 			// var_dump($list['allprice']);exit;
 		}else{
 			$list['allprice']=$allprice;
 			// var_dump($list['allprice']);exit;
 		}
 		

 		$list['ordernum']=$ordernum;
 		$list['user_id']=$userid;
 		$list['oname']=$oname;
 		$list['phone']=$phone;
 		$list['address']=$address;
 		
 		$list['time']=date('Y:m:d H:i:s',time());
 		// var_dump($list);
 		//获得最后插入的id
 		$id=Db::table("order")->insertGetId($list);
 		// var_dump($id);exit;
 		if($id){
 			//插入order_info表
 			//遍历SESSION表中的商品
 			foreach($_SESSION['order'] as $value){
 				$goodsid=$value['id'];
 				$price=$value['price'];
 				$orderid=$id;
 				$goodsname=$value['name'];
 				$qty=$value['num'];
				// $data1=array($goodsid,$goodsname,$price,$qty,$orderid);
				$data1['goods_id']=$goodsid;
				$data1['goods_name']=$goodsname;
				$data1['price']=$price;
				$data1['qty']=$qty;
				$data1['order_id']=$orderid;
				$data1['user_id']=Session::get('uid');

				$bool=Db::table("order_info")->insert($data1);
				$info=Db::table("pro_goods")->where('id',$goodsid)->select();
				$b=(int)$value['num'];
				
				$a=$info[0]['num']-$b;
				// var_dump($a);
				Db::table("pro_goods")->where('id',$goodsid)->update(['num'=>$a]);
				// $c=Db::table("pro_goods")->field("num")->where("id",$goodsid)->select();

 			}
 			if($bool){
 				$ide=Session::get('coupon');
 				// echo $id;exit;
 				if($ide!=null){
	 				$data=0;
	 				Db::execute("update coupon set statu={$data} where id={$ide}");
	 				// var_dump($a);
	 				$a=Session::delete('coupon');
	 			}
 				 unset($_SESSION['cart']);
 				 unset($_SESSION['order']); 				 
 				$this->success("购买成功","/orderinfo/index");
 			}else{
 				$this->error("购买失败","/cart/index");
 			}
 		}
	}

	//未选中地址提交
	public function geterror(){
		$this->error("未选中地址","/pay/index");
	}


}
